package ebcs.database.service.impl.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ebcs.database.bean.sys.PaRole;
import ebcs.database.bean.sys.PaUser;
import ebcs.database.bean.sys.PaUserRoleFunc;
import ebcs.database.mybatis.dao.sys.PaUserRoleFuncMapper;
import ebcs.database.service.sys.IUserFuncManagerService;
@Service
public class UserFuncManagerServiceImpl implements IUserFuncManagerService {
	
	@Autowired
	private PaUserRoleFuncMapper dao ;
	
	@Override
	public PaUserRoleFunc selectUserFuncByUserId(int userId) {
		PaUserRoleFunc record = new PaUserRoleFunc();
		record.setUserId(String.valueOf(userId));
		
		List<PaUserRoleFunc>  useFuncList = dao.selectUserFuncByUserId(record);
		List<PaUserRoleFunc> rsFuncList = new ArrayList(); 
		PaUserRoleFunc menu = new PaUserRoleFunc();
		menu.setFuncId("1");
		PaUserRoleFunc childrens = treeFuncList(useFuncList,menu);
		
		
		return childrens;
	}
	@Override
	public List<PaUserRoleFunc> selectUserFuncListByUserId(int userId) {
		PaUserRoleFunc record = new PaUserRoleFunc();
		record.setUserId(String.valueOf(userId));
		
		List<PaUserRoleFunc>  useFuncList = dao.selectUserFuncByUserId(record);
		
		
	    return useFuncList;
	}

	private PaUserRoleFunc treeFuncList(List<PaUserRoleFunc>  sourceList,PaUserRoleFunc rootMenu){
		
		if (sourceList == null)
        {
            return null;
        }
		List<PaUserRoleFunc> childList=new ArrayList<>();
		for(PaUserRoleFunc menu : sourceList) {
			 if(rootMenu.getFuncId().equals(menu.getParentId())){
				 PaUserRoleFunc menuChild = treeFuncList(sourceList, menu);
	                childList.add(menuChild);
	            }
		}
		if(childList.size()==0){
            return rootMenu;
        }
		
		rootMenu.setChildren(childList);
		
		
		return rootMenu;
	}
	
	@Override
	public List<PaUserRoleFunc> selectUserFuncAll(int userId) {
		// TODO Auto-generated method stub
		return dao.selectUserFuncAll(userId);
	}

	@Override
	public List<PaRole> selectRoleByUserId(int userId) {
		PaUser user = new PaUser();
		user.setUserId(userId);
		return dao.selectRoleByUserId(user);
	} 
 
}
